<?php
class ModelProjectProjectCustomer extends Model {
	public function addProjectCustomer($data) {
		$this->event->trigger('pre.admin.project_customer.add', $data);
		if(isset($data['contact_person_child'])){
			$contact_person_id_childs = implode(",",$data['contact_person_child']);
		}
		if(isset($data['shareholder1_child'])){
			$shareholder1_childs = implode(",",$data['shareholder1_child']);
		}
		if(isset($data['shareholder2_child'])){
			$shareholder2_childs = implode(",",$data['shareholder2_child']);
		}
		if(isset($data['shareholder3_child'])){
			$shareholder3_childs = implode(",",$data['shareholder3_child']);
		}
		if(isset($data['shareholder4_child'])){
			$shareholder4_childs = implode(",",$data['shareholder4_child']);
		}
		if(isset($data['shareholder5_child'])){
			$shareholder5_childs = implode(",",$data['shareholder5_child']);
		}
		// var_dump($shareholder1_childs);exit;
<<<<<<< HEAD
<<<<<<< HEAD
		$this->db->query("INSERT INTO " . DB_PREFIX . "project_customer SET company_eng_name = '" . $this->db->escape($data['company_eng_name']) . "', company_chn_name = '" . $this->db->escape($data['company_chn_name']) . "', contact_person_eng_name = '" . $this->db->escape($data['contact_person_eng_name']) . "', contact_person_chn_name = '" . $this->db->escape($data['contact_person_chn_name']) . "', contact_person_tel = '" . $this->db->escape($data['contact_person_tel']) . "', contact_person_email = '" . $this->db->escape($data['contact_person_email'])  . "', company_address = '" . $this->db->escape($data['company_address']) . "', contact_person_id = '" . $this->db->escape($contact_person_id_childs) . "', company_website = '" . $this->db->escape($data['company_website']) . "', shareholder1_eng_name = '" . $this->db->escape($data['shareholder1_eng_name']) . "', shareholder1_chn_name = '" . $this->db->escape($data['shareholder1_chn_name']) . "', shareholder1_id = '" . $this->db->escape($shareholder1_childs) . "', shareholder2_eng_name = '" . $this->db->escape($data['shareholder2_eng_name']) . "', shareholder2_chn_name = '" . $this->db->escape($data['shareholder2_chn_name']) . "', shareholder2_id = '" . $this->db->escape($shareholder2_childs) . "', shareholder3_eng_name = '" . $this->db->escape($data['shareholder3_eng_name']) . "', shareholder3_chn_name = '" . $this->db->escape($data['shareholder3_chn_name']) . "', shareholder3_id = '" . $this->db->escape($shareholder3_childs) . "', shareholder4_eng_name = '" . $this->db->escape($data['shareholder4_eng_name']) . "', shareholder4_chn_name = '" . $this->db->escape($data['shareholder4_chn_name']) . "', shareholder4_id = '" . $this->db->escape($shareholder4_childs) . "', shareholder5_eng_name = '" . $this->db->escape($data['shareholder5_eng_name']) . "', shareholder5_chn_name = '" . $this->db->escape($data['shareholder5_chn_name']) . "', shareholder5_id = '" . $this->db->escape($shareholder5_childs) . "', customer_status = '" . $this->db->escape($data['customer_status']) . "',  shareholder_share_1 = '" . $data['shareholder_share_1'] . "',  shareholder_share_2 = '" . $data['shareholder_share_2'] . "',  shareholder_share_3 = '" . $data['shareholder_share_3'] . "',  shareholder_share_4 = '" . $data['shareholder_share_4'] . "',  shareholder_share_5 = '" . $data['shareholder_share_5'] . "'");
=======
		$this->db->query("INSERT INTO " . DB_PREFIX . "project_customer SET company_eng_name = '" . $this->db->escape($data['company_eng_name']) . "', company_chn_name = '" . $this->db->escape($data['company_chn_name']) . "', contact_person_eng_name = '" . $this->db->escape($data['contact_person_eng_name']) . "', contact_person_chn_name = '" . $this->db->escape($data['contact_person_chn_name']) . "', contact_person_tel = '" . $this->db->escape($data['contact_person_tel']) . "', contact_person_email = '" . $this->db->escape($data['contact_person_email']) . "', contact_person_id = '" . $this->db->escape($contact_person_id_childs) . "', company_website = '" . $this->db->escape($data['company_website']) . "', shareholder1_eng_name = '" . $this->db->escape($data['shareholder1_eng_name']) . "', shareholder1_chn_name = '" . $this->db->escape($data['shareholder1_chn_name']) . "', shareholder1_id = '" . $this->db->escape($shareholder1_childs) . "', shareholder2_eng_name = '" . $this->db->escape($data['shareholder2_eng_name']) . "', shareholder2_chn_name = '" . $this->db->escape($data['shareholder2_chn_name']) . "', shareholder2_id = '" . $this->db->escape($shareholder2_childs) . "', shareholder3_eng_name = '" . $this->db->escape($data['shareholder3_eng_name']) . "', shareholder3_chn_name = '" . $this->db->escape($data['shareholder3_chn_name']) . "', shareholder3_id = '" . $this->db->escape($shareholder3_childs) . "', shareholder4_eng_name = '" . $this->db->escape($data['shareholder4_eng_name']) . "', shareholder4_chn_name = '" . $this->db->escape($data['shareholder4_chn_name']) . "', shareholder4_id = '" . $this->db->escape($shareholder4_childs) . "', shareholder5_eng_name = '" . $this->db->escape($data['shareholder5_eng_name']) . "', shareholder5_chn_name = '" . $this->db->escape($data['shareholder5_chn_name']) . "', shareholder5_id = '" . $this->db->escape($shareholder5_childs) . "', customer_status = '" . $this->db->escape($data['customer_status']) . "',  shareholder_share_1 = '" . $data['shareholder_share_1'] . "',  shareholder_share_2 = '" . $data['shareholder_share_2'] . "',  shareholder_share_3 = '" . $data['shareholder_share_3'] . "',  shareholder_share_4 = '" . $data['shareholder_share_4'] . "',  shareholder_share_5 = '" . $data['shareholder_share_5'] . "'");
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
		$this->db->query("INSERT INTO " . DB_PREFIX . "project_customer SET company_eng_name = '" . $this->db->escape($data['company_eng_name']) . "', company_chn_name = '" . $this->db->escape($data['company_chn_name']) . "', contact_person_eng_name = '" . $this->db->escape($data['contact_person_eng_name']) . "', contact_person_chn_name = '" . $this->db->escape($data['contact_person_chn_name']) . "', contact_person_tel = '" . $this->db->escape($data['contact_person_tel']) . "', contact_person_email = '" . $this->db->escape($data['contact_person_email']) . "', contact_person_id = '" . $this->db->escape($contact_person_id_childs) . "', company_website = '" . $this->db->escape($data['company_website']) . "', shareholder1_eng_name = '" . $this->db->escape($data['shareholder1_eng_name']) . "', shareholder1_chn_name = '" . $this->db->escape($data['shareholder1_chn_name']) . "', shareholder1_id = '" . $this->db->escape($shareholder1_childs) . "', shareholder2_eng_name = '" . $this->db->escape($data['shareholder2_eng_name']) . "', shareholder2_chn_name = '" . $this->db->escape($data['shareholder2_chn_name']) . "', shareholder2_id = '" . $this->db->escape($shareholder2_childs) . "', shareholder3_eng_name = '" . $this->db->escape($data['shareholder3_eng_name']) . "', shareholder3_chn_name = '" . $this->db->escape($data['shareholder3_chn_name']) . "', shareholder3_id = '" . $this->db->escape($shareholder3_childs) . "', shareholder4_eng_name = '" . $this->db->escape($data['shareholder4_eng_name']) . "', shareholder4_chn_name = '" . $this->db->escape($data['shareholder4_chn_name']) . "', shareholder4_id = '" . $this->db->escape($shareholder4_childs) . "', shareholder5_eng_name = '" . $this->db->escape($data['shareholder5_eng_name']) . "', shareholder5_chn_name = '" . $this->db->escape($data['shareholder5_chn_name']) . "', shareholder5_id = '" . $this->db->escape($shareholder5_childs) . "', customer_status = '" . $this->db->escape($data['customer_status']) . "',  shareholder_share_1 = '" . $data['shareholder_share_1'] . "',  shareholder_share_2 = '" . $data['shareholder_share_2'] . "',  shareholder_share_3 = '" . $data['shareholder_share_3'] . "',  shareholder_share_4 = '" . $data['shareholder_share_4'] . "',  shareholder_share_5 = '" . $data['shareholder_share_5'] . "'");
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa

		$project_customer_id = $this->db->getLastId();
		$this->event->trigger('post.admin.project_customer.add', $project_customer_id);

		return $project_customer_id;
	}

	public function editVendor($project_customer_id, $data) {
		$this->event->trigger('pre.admin.project_customer.edit', $data);
		if(isset($data['contact_person_child'])){
			$contact_person_id_childs = implode(",",$data['contact_person_child']);
		}
		if(isset($data['shareholder1_child'])){
			$shareholder1_childs = implode(",",$data['shareholder1_child']);
		}
		if(isset($data['shareholder2_child'])){
			$shareholder2_childs = implode(",",$data['shareholder2_child']);
		}
		if(isset($data['shareholder3_child'])){
			$shareholder3_childs = implode(",",$data['shareholder3_child']);
		}
		if(isset($data['shareholder4_child'])){
			$shareholder4_childs = implode(",",$data['shareholder4_child']);
		}
		if(isset($data['shareholder5_child'])){
			$shareholder5_childs = implode(",",$data['shareholder5_child']);
		}
<<<<<<< HEAD
<<<<<<< HEAD
		$this->db->query("UPDATE " . DB_PREFIX . "project_customer SET company_eng_name = '" . $this->db->escape($data['company_eng_name']) . "', company_chn_name = '" . $this->db->escape($data['company_chn_name']) . "', contact_person_eng_name = '" . $this->db->escape($data['contact_person_eng_name']) . "', contact_person_chn_name = '" . $this->db->escape($data['contact_person_chn_name']) . "', contact_person_tel = '" . $this->db->escape($data['contact_person_tel']) . "', contact_person_email = '" . $this->db->escape($data['contact_person_email']) . "', company_address = '" . $this->db->escape($data['company_address']) . "', contact_person_id = '" . $this->db->escape($contact_person_id_childs) . "', company_website = '" . $this->db->escape($data['company_website']) . "', shareholder1_eng_name = '" . $this->db->escape($data['shareholder1_eng_name']) . "', shareholder1_chn_name = '" . $this->db->escape($data['shareholder1_chn_name']) . "', shareholder1_id = '" . $this->db->escape($shareholder1_childs) . "', shareholder2_eng_name = '" . $this->db->escape($data['shareholder2_eng_name']) . "', shareholder2_chn_name = '" . $this->db->escape($data['shareholder2_chn_name']) . "', shareholder2_id = '" . $this->db->escape($shareholder2_childs) . "', shareholder3_eng_name = '" . $this->db->escape($data['shareholder3_eng_name']) . "', shareholder3_chn_name = '" . $this->db->escape($data['shareholder3_chn_name']) . "', shareholder3_id = '" . $this->db->escape($shareholder3_childs) . "', shareholder4_eng_name = '" . $this->db->escape($data['shareholder4_eng_name']) . "', shareholder4_chn_name = '" . $this->db->escape($data['shareholder4_chn_name']) . "', shareholder4_id = '" . $this->db->escape($shareholder4_childs) . "', shareholder5_eng_name = '" . $this->db->escape($data['shareholder5_eng_name']) . "', shareholder5_chn_name = '" . $this->db->escape($data['shareholder5_chn_name']) . "', shareholder5_id = '" . $this->db->escape($shareholder5_childs) . "', customer_status = '" . $this->db->escape($data['customer_status']) . "',  shareholder_share_1 = '" . $data['shareholder_share_1'] . "',  shareholder_share_2 = '" . $data['shareholder_share_2'] . "',  shareholder_share_3 = '" . $data['shareholder_share_3'] . "',  shareholder_share_4 = '" . $data['shareholder_share_4'] . "',  shareholder_share_5 = '" . $data['shareholder_share_5'] . "' WHERE project_customer_id = '" .(int)$project_customer_id. "'");
=======
		$this->db->query("UPDATE " . DB_PREFIX . "project_customer SET company_eng_name = '" . $this->db->escape($data['company_eng_name']) . "', company_chn_name = '" . $this->db->escape($data['company_chn_name']) . "', contact_person_eng_name = '" . $this->db->escape($data['contact_person_eng_name']) . "', contact_person_chn_name = '" . $this->db->escape($data['contact_person_chn_name']) . "', contact_person_tel = '" . $this->db->escape($data['contact_person_tel']) . "', contact_person_email = '" . $this->db->escape($data['contact_person_email']) . "', contact_person_id = '" . $this->db->escape($contact_person_id_childs) . "', company_website = '" . $this->db->escape($data['company_website']) . "', shareholder1_eng_name = '" . $this->db->escape($data['shareholder1_eng_name']) . "', shareholder1_chn_name = '" . $this->db->escape($data['shareholder1_chn_name']) . "', shareholder1_id = '" . $this->db->escape($shareholder1_childs) . "', shareholder2_eng_name = '" . $this->db->escape($data['shareholder2_eng_name']) . "', shareholder2_chn_name = '" . $this->db->escape($data['shareholder2_chn_name']) . "', shareholder2_id = '" . $this->db->escape($shareholder2_childs) . "', shareholder3_eng_name = '" . $this->db->escape($data['shareholder3_eng_name']) . "', shareholder3_chn_name = '" . $this->db->escape($data['shareholder3_chn_name']) . "', shareholder3_id = '" . $this->db->escape($shareholder3_childs) . "', shareholder4_eng_name = '" . $this->db->escape($data['shareholder4_eng_name']) . "', shareholder4_chn_name = '" . $this->db->escape($data['shareholder4_chn_name']) . "', shareholder4_id = '" . $this->db->escape($shareholder4_childs) . "', shareholder5_eng_name = '" . $this->db->escape($data['shareholder5_eng_name']) . "', shareholder5_chn_name = '" . $this->db->escape($data['shareholder5_chn_name']) . "', shareholder5_id = '" . $this->db->escape($shareholder5_childs) . "', customer_status = '" . $this->db->escape($data['customer_status']) . "',  shareholder_share_1 = '" . $data['shareholder_share_1'] . "',  shareholder_share_2 = '" . $data['shareholder_share_2'] . "',  shareholder_share_3 = '" . $data['shareholder_share_3'] . "',  shareholder_share_4 = '" . $data['shareholder_share_4'] . "',  shareholder_share_5 = '" . $data['shareholder_share_5'] . "' WHERE project_customer_id = '" .(int)$project_customer_id. "'");
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
		$this->db->query("UPDATE " . DB_PREFIX . "project_customer SET company_eng_name = '" . $this->db->escape($data['company_eng_name']) . "', company_chn_name = '" . $this->db->escape($data['company_chn_name']) . "', contact_person_eng_name = '" . $this->db->escape($data['contact_person_eng_name']) . "', contact_person_chn_name = '" . $this->db->escape($data['contact_person_chn_name']) . "', contact_person_tel = '" . $this->db->escape($data['contact_person_tel']) . "', contact_person_email = '" . $this->db->escape($data['contact_person_email']) . "', contact_person_id = '" . $this->db->escape($contact_person_id_childs) . "', company_website = '" . $this->db->escape($data['company_website']) . "', shareholder1_eng_name = '" . $this->db->escape($data['shareholder1_eng_name']) . "', shareholder1_chn_name = '" . $this->db->escape($data['shareholder1_chn_name']) . "', shareholder1_id = '" . $this->db->escape($shareholder1_childs) . "', shareholder2_eng_name = '" . $this->db->escape($data['shareholder2_eng_name']) . "', shareholder2_chn_name = '" . $this->db->escape($data['shareholder2_chn_name']) . "', shareholder2_id = '" . $this->db->escape($shareholder2_childs) . "', shareholder3_eng_name = '" . $this->db->escape($data['shareholder3_eng_name']) . "', shareholder3_chn_name = '" . $this->db->escape($data['shareholder3_chn_name']) . "', shareholder3_id = '" . $this->db->escape($shareholder3_childs) . "', shareholder4_eng_name = '" . $this->db->escape($data['shareholder4_eng_name']) . "', shareholder4_chn_name = '" . $this->db->escape($data['shareholder4_chn_name']) . "', shareholder4_id = '" . $this->db->escape($shareholder4_childs) . "', shareholder5_eng_name = '" . $this->db->escape($data['shareholder5_eng_name']) . "', shareholder5_chn_name = '" . $this->db->escape($data['shareholder5_chn_name']) . "', shareholder5_id = '" . $this->db->escape($shareholder5_childs) . "', customer_status = '" . $this->db->escape($data['customer_status']) . "',  shareholder_share_1 = '" . $data['shareholder_share_1'] . "',  shareholder_share_2 = '" . $data['shareholder_share_2'] . "',  shareholder_share_3 = '" . $data['shareholder_share_3'] . "',  shareholder_share_4 = '" . $data['shareholder_share_4'] . "',  shareholder_share_5 = '" . $data['shareholder_share_5'] . "' WHERE project_customer_id = '" .(int)$project_customer_id. "'");
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa

		$this->event->trigger('post.admin.project_customer.edit', $project_customer_id);
	}

	public function deleteVendor($project_customer_id) {
		$this->event->trigger('pre.admin.project_customer.delete', $project_customer_id);
		$query = $this->db->query("SELECT shareholder1_id,shareholder2_id,shareholder3_id,shareholder4_id,shareholder5_id FROM " . DB_PREFIX . "project_customer WHERE project_customer_id = '" . (int)$project_customer_id . "'");
		if(!empty($query->row['shareholder1_id'])){
			$file = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . (int)$query->row['shareholder1_id'] . ")");
			foreach ($file->rows as $key => $value) {
				if (is_file($value['file_url'])) {
					unlink($value['file_url']);
				} elseif (is_dir($value['file_url'])) {
					rmdir($value['file_url']);
				}
			}

		}
		if(!empty($query->row['shareholder2_id'])){
			$file = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . (int)$query->row['shareholder2_id'] . ")");
			foreach ($file->rows as $key => $value) {
				if (is_file($value['file_url'])) {
					unlink($value['file_url']);
				} elseif (is_dir($value['file_url'])) {
					rmdir($value['file_url']);
				}
			}

		}
		if(!empty($query->row['shareholder3_id'])){
			$file = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . (int)$query->row['shareholder3_id'] . ")");
			foreach ($file->rows as $key => $value) {
				if (is_file($value['file_url'])) {
					unlink($value['file_url']);
				} elseif (is_dir($value['file_url'])) {
					rmdir($value['file_url']);
				}
			}

		}
		if(!empty($query->row['shareholder4_id'])){
			$file = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . (int)$query->row['shareholder4_id'] . ")");
			foreach ($file->rows as $key => $value) {
				if (is_file($value['file_url'])) {
					unlink($value['file_url']);
				} elseif (is_dir($value['file_url'])) {
					rmdir($value['file_url']);
				}
			}

		}
		if(!empty($query->row['shareholder5_id'])){
			$file = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . (int)$query->row['shareholder5_id'] . ")");
			foreach ($file->rows as $key => $value) {
				if (is_file($value['file_url'])) {
					unlink($value['file_url']);
				} elseif (is_dir($value['file_url'])) {
					rmdir($value['file_url']);
				}
			}

		}
		$shareholder1_id = (!empty($query->row['shareholder1_id']))?$query->row['shareholder1_id']:0;
		$shareholder2_id = (!empty($query->row['shareholder2_id']))?$query->row['shareholder2_id']:0;
		$shareholder3_id = (!empty($query->row['shareholder3_id']))?$query->row['shareholder3_id']:0;
		$shareholder4_id = (!empty($query->row['shareholder4_id']))?$query->row['shareholder4_id']:0;
		$shareholder5_id = (!empty($query->row['shareholder5_id']))?$query->row['shareholder5_id']:0;

		$this->db->query("DELETE FROM `" . DB_PREFIX . "project_file` WHERE project_file_id in (" . $shareholder1_id.",".$shareholder2_id.",".$shareholder3_id.",".$shareholder4_id.",".$shareholder5_id. ")");

		$this->db->query("DELETE FROM `" . DB_PREFIX . "project_customer` WHERE project_customer_id = '" . (int)$project_customer_id . "'");

		$this->event->trigger('post.admin.project_customer.delete', $project_customer_id);
	}

	public function repairCategories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int)$parent_id . "'");

		foreach ($query->rows as $category) {
			// Delete the path below the current one
			$this->db->query("DELETE FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$category['category_id'] . "'");

			// Fix for records with no paths
			$level = 0;

			$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$parent_id . "' ORDER BY level ASC");

			foreach ($query->rows as $result) {
				$this->db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET category_id = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$result['path_id'] . "', level = '" . (int)$level . "'");

				$level++;
			}

			$this->db->query("REPLACE INTO `" . DB_PREFIX . "category_path` SET category_id = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$category['category_id'] . "', level = '" . (int)$level . "'");

			$this->repairCategories($category['category_id']);
		}
	}

	public function getpProjectCustomers($project_customer_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_customer WHERE project_customer_id = '" . (int)$project_customer_id . "'");
		if(!empty($query->row['contact_person_id'])){
			$queryID = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $query->row['contact_person_id'] . ")");
			}
		if(!empty($query->row['shareholder1_id'])){
			$query1 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $query->row['shareholder1_id'] . ")");
			}
		if(!empty($query->row['shareholder2_id'])){
			$query2 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $query->row['shareholder2_id'] . ")");
			}
		if(!empty($query->row['shareholder3_id'])){
			$query3 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $query->row['shareholder3_id'] . ")");
			}
		if(!empty($query->row['shareholder4_id'])){
			$query4 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $query->row['shareholder4_id'] . ")");
			}
		if(!empty($query->row['shareholder5_id'])){
			$query5 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $query->row['shareholder5_id'] . ")");
			}

		$query->row['data'] = array(
				'contact_person_id' => (!empty($queryID->rows))?$queryID->rows:'',
				'shareholder1' => (!empty($query1->rows))?$query1->rows:'',
				'shareholder2' => (!empty($query2->rows))?$query2->rows:'',
				'shareholder3' => (!empty($query3->rows))?$query3->rows:'',
				'shareholder4' => (!empty($query4->rows))?$query4->rows:'',
				'shareholder5' => (!empty($query5->rows))?$query5->rows:'',

		);

		return $query->row;
	}

	public function getProjectCustomer($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "project_customer WHERE 1=1 ";

		if (!empty($data['filter_company_eng_name'])) {
			$sql .= " AND company_eng_name LIKE '" . $this->db->escape($data['filter_company_eng_name']) . "%'";
		}

		if (!empty($data['filter_company_chn_name'])) {
			$sql .= " AND company_chn_name LIKE '" . $this->db->escape($data['filter_company_chn_name']) . "%'";
		}


		$sql .= " GROUP BY project_customer_id ";

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else if(isset($data['order']) && ($data['order'] == 'ASC')){
			$sql .= " ASC";
		} else {
			$sql .= " DESC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);
		foreach ($query->rows as $key => $value) {
			$contact_person_id = (!empty($value['contact_person_id']))?$value['contact_person_id']:0;
			$shareholder1 = (!empty($value['shareholder1_id']))?$value['shareholder1_id']:0;
			$shareholder2 = (!empty($value['shareholder2_id']))?$value['shareholder2_id']:0;
			$shareholder3 = (!empty($value['shareholder3_id']))?$value['shareholder3_id']:0;
			$shareholder4 = (!empty($value['shareholder4_id']))?$value['shareholder4_id']:0;
			$shareholder5 = (!empty($value['shareholder5_id']))?$value['shareholder5_id']:0;

			$queryID = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $contact_person_id . ")");
			$query1 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $shareholder1 . ")");
			$query2 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $shareholder2 . ")");
			$query3 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $shareholder3 . ")");
			$query4 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $shareholder4 . ")");
			$query5 = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (" . $shareholder5 . ")");
			$query->rows[$key]['data'] = array(
					'contact_person_id' => (!empty($queryID->rows))?$queryID->rows:'',
					'shareholder1' => (!empty($query1->rows))?$query1->rows:'',
					'shareholder2' => (!empty($query5->rows))?$query5->rows:'',
					'shareholder3' => (!empty($query6->rows))?$query6->rows:'',
					'shareholder4' => (!empty($query7->rows))?$query7->rows:'',
					'shareholder5' => (!empty($query8->rows))?$query8->rows:'',

				);
		}

		return $query->rows;
	}

	public function getCategoryDescriptions($category_id) {
		$category_description_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_description WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_title'       => $result['meta_title'],
				'meta_description' => $result['meta_description'],
				'meta_keyword'     => $result['meta_keyword'],
				'description'      => $result['description']
			);
		}

		return $category_description_data;
	}

	public function getCategoryFilters($category_id) {
		$category_filter_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_filter WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_filter_data[] = $result['filter_id'];
		}

		return $category_filter_data;
	}

	public function getCategoryStores($category_id) {
		$category_store_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_to_store WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_store_data[] = $result['store_id'];
		}

		return $category_store_data;
	}

	public function getCategoryLayouts($category_id) {
		$category_layout_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_layout_data[$result['store_id']] = $result['layout_id'];
		}

		return $category_layout_data;
	}

	public function getTotalCategories($data = array()) {
		$sql = "SELECT  COUNT(*) AS total FROM " . DB_PREFIX . "project_customer WHERE 1=1 ";

		if (!empty($data['filter_company_eng_name'])) {
			$sql .= " AND company_eng_name LIKE '" . $this->db->escape($data['filter_company_eng_name']) . "%'";
		}

		if (!empty($data['filter_company_chn_name'])) {
			$sql .= " AND company_chn_name LIKE '" . $this->db->escape($data['filter_company_chn_name']) . "%'";
		}
		$query = $this->db->query($sql);

		return $query->row['total'];
	}
	
	public function getTotalCategoriesByLayoutId($layout_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category_to_layout WHERE layout_id = '" . (int)$layout_id . "'");

		return $query->row['total'];
	}
	public function getVendorChecklists($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "project_customer WHERE 1=1 ";

		if (!empty($data['filter_company_eng_name'])) {
			$sql .= " AND company_eng_name LIKE '" . $this->db->escape($data['filter_company_eng_name']) . "%'";
		}

		if (!empty($data['filter_company_chn_name'])) {
			$sql .= " AND company_chn_name LIKE '" . $this->db->escape($data['filter_company_chn_name']) . "%'";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		// var_dump($query->rows);exit;
		return $query->rows;
	}


	public function clearFile($project_customer_id,$no_name,$name){
		// var_dump($project_customer_id,$no_name,$name);exit;
		if(isset($project_customer_id)){
		$this->db->query("UPDATE " . DB_PREFIX . "project_customer SET `".$no_name."_id` = ''  WHERE project_customer_id = '" . (int)$project_customer_id."'");
		}
		$query = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id = '" . (int)$name."'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "project_file WHERE project_file_id = '" . (int)$name."'");
		// var_dump($query->row['file_url']);exit;
		return $query->row['file_url'];
	}

	public function checkshare($shareholder_name,$project_customer_id = false){
		if($project_customer_id){
			$sql = "SELECT * FROM " . DB_PREFIX . "project_customer WHERE project_customer_id != '" . (int)$project_customer_id . "' AND shareholder1_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_1 > '30' OR project_customer_id != '" . (int)$project_customer_id . "' AND shareholder2_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_2 > '30' OR project_customer_id != '" . (int)$project_customer_id . "' AND  shareholder3_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_3 > '30' OR project_customer_id != '" . (int)$project_customer_id . "' AND shareholder4_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_4 > '30' OR project_customer_id != '" . (int)$project_customer_id . "' AND  shareholder5_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_5 > '30' ";
			// var_dump($sql);
		}else {
			
			$sql = "SELECT * FROM " . DB_PREFIX . "project_customer WHERE shareholder1_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_1 > '30' OR shareholder2_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_2 > '30' OR shareholder3_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_3 > '30' OR shareholder4_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_4 > '30' OR shareholder5_chn_name = '" . $this->db->escape($shareholder_name) . "' AND shareholder_share_5 > '30' ";
			// var_dump($sql);
		}

		$query = $this->db->query($sql);
		if($query->num_rows > 0){
				return $query->rows[0]['company_eng_name'];
		}else{
			return false;
		}
	}
<<<<<<< HEAD
<<<<<<< HEAD


	public function checkAddress($company_address,$project_customer_id = false){
		if($project_customer_id){
			$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "project_customer` WHERE company_address = '" . $this->db->escape($company_address) . "' AND $project_customer_id != '" . $this->db->escape($company_address) . "'");
			if($query->num_rows > 0){
					return $query->rows[0]['company_eng_name'];
			}else{
				return false;
			}
		}
	}



=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
}
